# Contributor: Clayton Craft <clayton@craftyguy.net>
# Maintainer: Clayton Craft <clayton@craftyguy.net>
pkgname=mercury
pkgver=22.01.8
pkgrel=1
pkgdesc="Compiler for the Mercury language"
url="https://www.mercurylang.org"
arch="all"
license="GPL-2.0-only"
makedepends="autoconf automake bison flex"
depends="$pkgname-libs"
subpackages="$pkgname-doc $pkgname-libs"
source="
	https://dl.mercurylang.org/release/mercury-srcdist-$pkgver.tar.gz
	0001-disable-gcc-label-support-on-arm.patch
	0002-always-build-shared-libs.patch
	"
options="!check"  # tests are unstable / flaky
builddir="$srcdir"/mercury-srcdist-"$pkgver"

build() {
	# TODO: The configure check for GCC labels causes a bug in qemu
	# on riscv64. Need to create a minimal example and report that
	# upstream. As a workaround, just skip the check by hardcoding
	# the result.
	mercury_cv_gcc_labels=yes ./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--enable-shared-libs \
		--with-llds-base-grade=fast \
		--enable-libgrades="hlc.gc"

	make PARALLEL="-j${JOBS:-1}"
}

package() {
	make DESTDIR="$pkgdir" \
		INSTALL_PREFIX="$pkgdir"/usr \
		INSTALL_MAN_DIR="$pkgdir"/usr/share/man \
		INSTALL_INFO_DIR="$pkgdir"/usr/share/info \
		MERCURY_COMPILER="$pkgdir"/usr/bin/mercury_compile \
		MERCURY_CONFIG_DIR="$pkgdir"/usr/lib/mercury \
		PARALLEL="-j${JOBS:-1}" \
		install

	rm "$pkgdir"/usr/lib/mercury/lib/hlc.gc/libmer_*.a
	rm "$pkgdir"/usr/lib/mercury/lib/libgc.a
	rm "$pkgdir"/usr/bin/*.bat
}

libs() {
	pkgdesc="Mercury shared library"
	default_libs
	amove usr/lib/mercury/lib/*/*.so
	amove usr/lib/mercury/lib/*.so
}

sha512sums="
7a3f40c8876bec059842d133b50be9c19d60739ba320ce5f0df160b2e3473077f4394e794a45e23d35f4088dc04f23a24c8f6e0f724bbcf4b3822e15e5d3bf72  mercury-srcdist-22.01.8.tar.gz
57af805aa1a4bb7e7b471ffd5c2311138385ec76107568a0f7846e50922a8aca7604889be1f181b86fb5b04f68225bb588184746962757a1d3b59133d9280b0a  0001-disable-gcc-label-support-on-arm.patch
e8c546928d4872d386a754e9d5289939577b32ed16a9f2e0b3cce5b36cd3aeee113265d2435188ab9bbc7f57bd355ea7dc9837be935b877488d44f5e67783332  0002-always-build-shared-libs.patch
"
